package com.example;

import java.util.*;

public class B3 {

    private enum State {
        IDLE, IN_STRING, ESCAPE, END_STRING
    }

    // 用于模拟用户输入的密码验证过程
    private enum PasswordState {
        WAITING_FOR_PASSWORD, VERIFYING, CORRECT, INCORRECT, LOCKED
    }

    private State currentState;
    private StringBuilder currentString;
    private PasswordState passwordState;
    private final String correctPassword = "1234"; // 设定的正确密码
    private int failedAttempts;
    private static final int MAX_ATTEMPTS = 3;

    public B3() {
        this.currentState = State.IDLE;
        this.currentString = new StringBuilder();
        this.passwordState = PasswordState.WAITING_FOR_PASSWORD;
        this.failedAttempts = 0;
    }

    // 模拟输入字符并进行密码验证
    public void enterCharacter(char c) {
        if (passwordState == PasswordState.WAITING_FOR_PASSWORD || passwordState == PasswordState.INCORRECT) {
            if (c == '\n') { // 假设按下回车键表示完成输入
                verifyPassword();
            } else {
                currentString.append(c);
                System.out.println("当前输入: " + currentString);
            }
        }
    }

    // 验证密码
    private void verifyPassword() {
        passwordState = PasswordState.VERIFYING;
        String enteredPassword = currentString.toString();
        if (enteredPassword.equals(correctPassword)) {
            passwordState = PasswordState.CORRECT;
            System.out.println("密码正确，进入功能选择。");
        } else {
            passwordState = PasswordState.INCORRECT;
            failedAttempts++;
            if (failedAttempts >= MAX_ATTEMPTS) {
                lockControlPanel();
            } else {
                System.out.println("密码错误，请重试。");
                currentString.setLength(0); // 清空输入
            }
        }
    }

    // 锁定控制面板
    private void lockControlPanel() {
        passwordState = PasswordState.LOCKED;
        System.out.println("连续三次密码错误，控制面板被锁定，您可以稍后再试。");
    }

    public static void main(String[] args) {
        B3 b3 = new B3();

        // 模拟用户输入正确的密码
        System.out.println("模拟用户输入正确密码：");
        b3.enterCharacter('1');
        b3.enterCharacter('2');
        b3.enterCharacter('3');
        b3.enterCharacter('4');
        b3.enterCharacter('\n'); // 用户按下回车键

        // 模拟密码错误的情况
        System.out.println("\n模拟用户输入错误密码：");
        b3.enterCharacter('1');
        b3.enterCharacter('2');
        b3.enterCharacter('5');
        b3.enterCharacter('6');
        b3.enterCharacter('\n'); // 用户按下回车键

        b3.enterCharacter('1');
        b3.enterCharacter('2');
        b3.enterCharacter('7');
        b3.enterCharacter('8');
        b3.enterCharacter('\n'); // 用户按下回车键

        b3.enterCharacter('1');
        b3.enterCharacter('2');
        b3.enterCharacter('9');
        b3.enterCharacter('0');
        b3.enterCharacter('\n'); // 用户按下回车键
    }
}
